package pb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import util.ReadLineUtil;

/**
 * Created by gamy on 2017/8/3.
 */
public class ProtoBufParser {

    ArrayList<PBMessage> messages;
    String pbFile;




    public ProtoBufParser(String pbFile) {
        this.pbFile = pbFile;
    }

    public ArrayList<PBMessage> parse() {

        ArrayList<PBMessage> messages = new ArrayList<PBMessage>();

        ReadLineUtil.readLine(this.pbFile, new ReadLineUtil.ReadLineHandler() {

                    PBMessage msg = null;

                    @Override
                    public void begin() {

                    }

                    @Override
                    public void handle(String lineStr) {

                        //如果找到message
                        if (PBMessage.isMessageLineString(lineStr)) {
                            msg = new PBMessage(lineStr);
                            messages.add(msg);
                        }

                        //如果是行
                        if (PBField.isFieldLine(lineStr)) {
                            PBField field = new PBField(lineStr);
                            msg.addField(field);
                        }
                    }

                    @Override
                    public void end() {
                        for (PBMessage message : messages) {
                            System.out.println(message);
                        }
                    }
                }

        );

        return messages;
    }
}
